Welcome to Expert Viewpoints: Using anonymous
callback function in Node.js. In this video, we will hear from several
application development professionals discussing when they found it useful to know how to use anonymous callback functions in Node.js and
how they were key to the solutions they developed. Anything in Node.js that
requires you to make a request outside your application, which most likely you will, understanding how callbacks work is very important. So when we say anonymous callbacks, callbacks
don't have to be anonymous. In fact, a lot of times I will write named
functions and pass that as arguments into my functions that need the callback. I'll give you an example. So let's say you're building a website that
displays temperature on the main website, right. So in order to get the temperature, you're
most likely calling another website, most likely another web service, that accepts some JSON as input and gives
you back another JSON as output. Now, when the end user comes on your website,
they're scrolling up and down. If you block that and make the request to
the weather API, then your user will not be able to even scroll up and down, right. So in the browser, it's doing one thing at
a time. Node.js is single-threaded, and so everything
gets blocked if you're making that request. And if you think about it, if the weather
API is not giving your response, for whatever reason, maybe your credentials are incorrect, maybe the server is down, then your users
are left waiting. In that case, the callback functions come
in handy. So what you do is you make a request to the
weather API, and then you go about your business. And when a response comes back, you can handle
that response and show the information to the user. So, understanding callbacks is (a)key to mastering
JavaScript. Functions are a first class object in JavaScript
and can therefore be passed as parameters to other functions. A callback function specifically follows the
callback pattern and involves passing a function as a parameter to another function, and the
callback function is invoked once the enclosing function has executed. And so a callback function is a closure in
that the callback function has access to the containing function's scope variables. Callback functions can either be anonymous
or named, and here we're talking about anonymous callback functions. Anonymous functions are sometimes more readable
and are a good solution when the callback function is not intended to be reused. In other cases, a utility module like Async.js
can help cope with asynchronous JavaScript. Anonymous callback functions in JavaScript
are great. They are very useful to keep your code clean
and define a function just on the fly. So I've used them extensively when dealing
with messaging. The idea is that I'm sending messages and
receiving messages, and I want to process those messages. I don't always necessarily want to define
a full function for how to process it every single time. I just want to say hey, when I get this message,
I'm going to do something really simple. So, oftentimes, it's very easy and very useful
to just create a very simple anonymous callback function which gets a message, processes it,
does something, and then I never have to worry about it again. If you're planning on using Node.js for web
development, the concept of callback functions is one of
the first things you'll need to grasp. When you're defining routes in your web server, you'll be relying on a library to provide
you with request and response objects. These will be provided as arguments to your
callback function which will then read the request and modify
the response to make sure the client gets the desired result. While it's possible to use named functions
for your callback functions here, anonymous functions are often preferred because
each route's logic is unique and not duplicated elsewhere. So using anonymous functions keeps your code
clean and readable. Anonymous callback functions allow you to
write a block of scoped code that you can use again at a later time. Anonymous functions combined with the arrow
syntax allow us to write clear and concise pipelines in JavaScript to manipulate data
inside arrays.